package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.log.MongoLogHelp;
import com.xbongbong.paas.log.constant.LogMemoConstant;
import com.xbongbong.paas.log.pojo.InfoArrPojo;
import com.xbongbong.paas.pojo.ExcelLogPojo;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.service.ExcelLogService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.paas.util.ExcelLogUtil;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.OperateModuleTypeEnum;
import com.xbongbong.saas.enums.OperateTypeEnum;
import com.xbongbong.saas.enums.PayBalanceEnum;
import com.xbongbong.saas.enums.WriteOffRecordEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.*;
import com.xbongbong.saas.enums.product.PriceSetEnum;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/08/05 16:10
 */
@Service("excelLogService")
public class ExcelLogServiceImpl implements ExcelLogService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelLogServiceImpl.class);

    @Resource
    private MongoLogHelp mongoLogHelp;


    @Override
    public ExcelLogPojo logForExportFormData(XbbRefTypeEnum xbbRefTypeEnum, PaasFormDataEsListVO formDataListVO, FormDataListDTO formDataListDTO) throws XbbException {
        List<PaasFormDataEntityExt> paasFormDataESList = formDataListVO.getPaasFormDataESList();
        OperateModuleTypeEnum operateModuleTypeEnum = OperateModuleTypeEnum.UNKNOWN;
        String typeName = XbbRefTypeEnum.UNKNOWN.getName();
        List<String> logName = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        String logMemo;
        switch (xbbRefTypeEnum) {
            case CONTRACT:
                operateModuleTypeEnum = OperateModuleTypeEnum.CONTRACT;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt contractES : paasFormDataESList){
                    StringBuilder sb = new StringBuilder();
                    String serialNo = contractES.getSerialNo();
                    JSONObject data = contractES.getData();
                    String name = data.getString(ContractEnum.NAME.getAttr());
                    String log = name == null ? serialNo : sb.append(serialNo).append("（").append(name).append("）").toString();
                    logName.add(log);
                }
                break;
            case QUOTATION:
                operateModuleTypeEnum = OperateModuleTypeEnum.QUOTATION;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt quotationES : paasFormDataESList){
                    StringBuilder sb = new StringBuilder();
                    String serialNo = quotationES.getSerialNo();
                    JSONObject data = quotationES.getData();
                    String name = data.getString(QuotationEnum.NAME.getAttr());
                    String log = name == null ? serialNo : sb.append(serialNo).append("（").append(name).append("）").toString();
                    logName.add(log);
                }
                break;
            case CUSTOMER_MANAGEMENT:
                XbbRefTypeEnum subRefTypeEnum = XbbRefTypeEnum.getByCode(formDataListDTO.getSubBusinessType());
                // 生成日志(导出)
                operateModuleTypeEnum = OperateModuleTypeEnum.CUSTOMER;
                typeName = subRefTypeEnum.getName();
                for(PaasFormDataEntityExt customerES : paasFormDataESList){
                    JSONObject data = customerES.getData();
                    String name = FastJsonHelper.getStringOrDefaultFromFormData(data, CustomerManagementEnum.NAME.getAttr(), "");
                    logName.add(name);
                }
                break;
            case SALES_OPPORTUNITY:
                operateModuleTypeEnum = OperateModuleTypeEnum.OPPORTUNITY;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    StringBuilder sb = new StringBuilder();
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    JSONObject data = paasFormDataEntityExt.getData();
                    String name = data.getString(SalesOpportunityEnum.NAME.getAttr());
                    String log = name == null ? serialNo : sb.append(serialNo).append("（").append(name).append("）").toString();
                    logName.add(log);
                }
                break;
            case CONTACT:
                operateModuleTypeEnum = OperateModuleTypeEnum.CONTACT;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    JSONObject data = paasFormDataEntityExt.getData();
                    String name = FastJsonHelper.getStringOrDefaultFromFormData(data, ContactEnum.NAME.getAttr(), "");
                    logName.add(name);
                }
                break;
            case CUSTOMER_COMMUNICATE:
                operateModuleTypeEnum = OperateModuleTypeEnum.CustomerCommunicate;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    JSONObject data = paasFormDataEntityExt.getData();
                    String name = FastJsonHelper.getStringOrDefaultFromFormData(data, CustomerCommunicateEnum.CUSTOMER_NAME_LINK_TEXT.getAttr(), "");
                    logName.add(name);
                }
                break;
            case SUPPLIER_COMMUNICATE:
                operateModuleTypeEnum = OperateModuleTypeEnum.SUPPLIER_COMMUNICATE;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    JSONObject data = paasFormDataEntityExt.getData();
                    String name = FastJsonHelper.getStringOrDefaultFromFormData(data, SupplierCommunicateEnum.SUPPLIER_NAME_LINK_TEXT.getAttr(), "");
                    logName.add(name);
                }
                break;
            case INVOICE:
                operateModuleTypeEnum = OperateModuleTypeEnum.INVOICE;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case PURCHASE_INVOICE:
                operateModuleTypeEnum = OperateModuleTypeEnum.PURCHASE_INVOICE;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case PAYMENT:
                operateModuleTypeEnum = OperateModuleTypeEnum.PaymentManage;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case PAY_PLAN:
                operateModuleTypeEnum = OperateModuleTypeEnum.PayManage;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case SUPPLIER:
                operateModuleTypeEnum = OperateModuleTypeEnum.SUPPLIER;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    JSONObject data = paasFormDataEntityExt.getData();
                    String name = FastJsonHelper.getStringOrDefaultFromFormData(data, SupplierEnum.SUPPLIER_NAME.getAttr(), "");
                    logName.add(name);
                }
                break;
            case SUPPLIER_CONTACT:
                operateModuleTypeEnum = OperateModuleTypeEnum.SUPPLIER_CONTACT;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    JSONObject data = paasFormDataEntityExt.getData();
                    String name = FastJsonHelper.getStringOrDefaultFromFormData(data, SupplierContactEnum.NAME.getAttr(), "");
                    logName.add(name);
                }
                break;
            case INSTOCK:
            case OTHER_INSTOCK:
                operateModuleTypeEnum = OperateModuleTypeEnum.INSTOCK;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case PURCHASE_INSTOCK:
                operateModuleTypeEnum = OperateModuleTypeEnum.PURCHASE_INSTOCK;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case REFUND_INSTOCK:
                operateModuleTypeEnum = OperateModuleTypeEnum.REFUND_INSTOCK;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case PRODUCTION_INSTOCK:
                operateModuleTypeEnum = OperateModuleTypeEnum.PRODUCTION_INSTOCK;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case RETURNED_MATERIEL_INSTOCK:
                operateModuleTypeEnum = OperateModuleTypeEnum.RETURNED_MATERIEL_INSTOCK;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case OUTSTOCK:
            case OTHER_OUTSTOCK:
                operateModuleTypeEnum = OperateModuleTypeEnum.OUTSTOCK;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case CONTRACT_OUTSTOCK:
                operateModuleTypeEnum = OperateModuleTypeEnum.CONTRACT_OUTSTOCK;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case RETURNED_PURCHASE_OUTSTOCK:
                operateModuleTypeEnum = OperateModuleTypeEnum.RETURNED_PURCHASE_OUTSTOCK;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case PRODUCTION_MATERIEL_OUTSTOCK:
                operateModuleTypeEnum = OperateModuleTypeEnum.PRODUCTION_MATERIEL_OUTSTOCK;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case WORK_ORDER_OUTSTOCK:
                operateModuleTypeEnum = OperateModuleTypeEnum.WORK_ORDER_OUTSTOCK;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case ASSEMBLE:
                operateModuleTypeEnum = OperateModuleTypeEnum.ASSEMBLE;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case TRANSFER:
                operateModuleTypeEnum = OperateModuleTypeEnum.TRANSFER;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case INVENTORY:
                operateModuleTypeEnum = OperateModuleTypeEnum.INVENTORY;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case RETURNED_PURCHASE:
                operateModuleTypeEnum = OperateModuleTypeEnum.RETURN_PURCHASE;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case PURCHASE:
                operateModuleTypeEnum = OperateModuleTypeEnum.PURCHASE;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    StringBuilder sb = new StringBuilder();
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    JSONObject data = paasFormDataEntityExt.getData();
                    String name = data.getString(PurchaseEnum.PURCHASE_NAME.getAttr());
                    String log = name == null ? serialNo : sb.append(serialNo).append("（").append(name).append("）").toString();
                    logName.add(log);
                }
                break;
            case PRODUCT:
                operateModuleTypeEnum = OperateModuleTypeEnum.PRODUCT;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    StringBuilder sb = new StringBuilder();
                    JSONObject data = paasFormDataEntityExt.getData();
                    String productNo = FastJsonHelper.getStringOrDefaultFromFormData(data, ProductEnum.PRODUCT_NO.getAttr(), "");
                    String name = data.getString(ProductEnum.NAME.getAttr());
                    String log = name == null ? productNo : sb.append(name).append("（").append(productNo).append("）").toString();
                    logName.add(log);
                }
                break;
            case BOM_BILL:
                operateModuleTypeEnum = OperateModuleTypeEnum.BOMBILL;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    StringBuilder sb = new StringBuilder();
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    JSONObject data = paasFormDataEntityExt.getData();
                    String name = data.getString(BomBillEnum.NAME.getAttr());
                    String log = name == null ? serialNo : sb.append(serialNo).append("（").append(name).append("）").toString();
                    logName.add(log);
                }
                break;
            case PRODUCTION_ORDER:
                operateModuleTypeEnum = OperateModuleTypeEnum.PRODUCTION_ORDER;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case REFUND:
                operateModuleTypeEnum = OperateModuleTypeEnum.REFUND;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    StringBuilder sb = new StringBuilder();
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    JSONObject data = paasFormDataEntityExt.getData();
                    String name = data.getString(RefundEnum.REFUND_NAME.getAttr());
                    String log = name == null ? serialNo : sb.append(serialNo).append("（").append(name).append("）").toString();
                    logName.add(log);
                }
                break;
            case STOCK_SEARCH:
                XbbRefTypeEnum refTypeEnum = XbbRefTypeEnum.getByCode(formDataListDTO.getSubBusinessType());
                operateModuleTypeEnum = OperateModuleTypeEnum.PRODUCT;
                typeName = Objects.isNull(refTypeEnum) ? "" : refTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    JSONObject data = paasFormDataEntityExt.getData();
                    String productNo = FastJsonHelper.getStringOrDefaultFromFormData(data, ProductEnum.PRODUCT_NO.getAttr(), "");
                    String name = data.getString(ProductEnum.NAME.getAttr());
                    StringBuilder sb = new StringBuilder();
                    name = name == null ? productNo : sb.append(name).append("（").append(productNo).append("）").toString();
                    logName.add(name);
                }
                break;
            case PAYMENT_SHEET:
                operateModuleTypeEnum = OperateModuleTypeEnum.PAYMENT_SHEET;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case PAY_SHEET:
                operateModuleTypeEnum = OperateModuleTypeEnum.PAY_SHEET;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case PREPAYMENT_BALANCE:
                operateModuleTypeEnum = OperateModuleTypeEnum.PREPAYMENT_BALANCE;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getData().getString(PaymentBalanceEnum.CUSTOMER_NAME.getAttr());
                    logName.add(serialNo);
                }
                break;
            case PREPAY_BALANCE:
                operateModuleTypeEnum = OperateModuleTypeEnum.PREPAY_BALANCE;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getData().getString(PayBalanceEnum.SUPPLIER_NAME.getAttr());
                    logName.add(serialNo);
                }
                break;
            case SEQ_SEARCH:
                operateModuleTypeEnum = OperateModuleTypeEnum.SEQ_SEARCH;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getData().getString(ProductSerialBalanceEnum.SERIALNO.getAttr());
                    String name = paasFormDataEntityExt.getData().getString(ProductSerialBalanceEnum.NAME.getAttr());
                    StringBuilder sb = new StringBuilder();
                    name = name == null ? serialNo : sb.append(name).append("（").append(serialNo).append("）").toString();
                    logName.add(name);
                }
                break;
            case BATCH_FLOW_BILL:
                operateModuleTypeEnum = OperateModuleTypeEnum.BATCH_FLOW_BILL;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getData().getString(BatchFlowBillEnum.SERIAL_NO.getAttr());
                    String name = paasFormDataEntityExt.getData().getString(BatchFlowBillEnum.NAME.getAttr());
                    StringBuilder sb = new StringBuilder();
                    name = name == null ? serialNo : sb.append(name).append("（").append(serialNo).append("）").toString();
                    logName.add(name);
                }
                break;
            case STOCK_FLOW_BILL:
                operateModuleTypeEnum = OperateModuleTypeEnum.STOCK_FLOW_BILL;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getData().getString(StockFlowBillEnum.SERIALNO.getAttr());
                    String name = paasFormDataEntityExt.getData().getString(StockFlowBillEnum.NAME.getAttr());
                    StringBuilder sb = new StringBuilder();
                    name = name == null ? serialNo : sb.append(name).append("（").append(serialNo).append("）").toString();
                    logName.add(name);
                }
                break;
            case STOCK_STANDING_BOOK:
                operateModuleTypeEnum = OperateModuleTypeEnum.STOCK_STANDING_BOOK;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getData().getString(StockStandingBookEnum.SERIALNO.getAttr());
                    String name = paasFormDataEntityExt.getData().getString(StockStandingBookEnum.NAME.getAttr());
                    StringBuilder sb = new StringBuilder();
                    name = name == null ? serialNo : sb.append(name).append("（").append(serialNo).append("）").toString();
                    logName.add(name);
                }
                break;
            case CLUE:
                XbbRefTypeEnum subClueEnum = XbbRefTypeEnum.getByCode(formDataListDTO.getSubBusinessType());
                // 生成日志(导出)
                operateModuleTypeEnum = OperateModuleTypeEnum.CLUE;
                typeName = subClueEnum.getName();
                for(PaasFormDataEntityExt clueES : paasFormDataESList){
                    JSONObject data = clueES.getData();
                    String name = FastJsonHelper.getStringOrDefaultFromFormData(data, ClueEnum.COMPANY_NAME.getAttr(), "");
                    logName.add(name);
                }
                break;
            case MARKET_ACTIVITY:
                operateModuleTypeEnum = OperateModuleTypeEnum.MARKET_ACTIVITY;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    JSONObject data = paasFormDataEntityExt.getData();
                    String name = FastJsonHelper.getStringOrDefaultFromFormData(data, MarketActivityEnum.NAME.getAttr(), "");
                    logName.add(name);
                }
                break;
            case PRICE_SET_PRODUCT:
                operateModuleTypeEnum = OperateModuleTypeEnum.STOCK_STANDING_BOOK;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
//                    String serialNo = paasFormDataEntityExt.getData().getString(StockStandingBookEnum.SERIALNO.getAttr());
                    String name = paasFormDataEntityExt.getData().getString(PriceSetEnum.NAME.getAttr());
//                    StringBuilder sb = new StringBuilder();
//                    name = name == null ? serialNo : sb.append(name).append("（").append(serialNo).append("）").toString();
                    logName.add(name);
                }
                break;
            case MARKETING_ACTIVITY_MANAGEMENT:
                operateModuleTypeEnum = OperateModuleTypeEnum.MARKETING_ACTIVITY_MANAGEMENT;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String name = paasFormDataEntityExt.getData().getString(MarketingActivityManagementEnum.NAME.getAttr());
                    logName.add(name);
                }
                break;
            case WRITE_OFF_RECORD:
                operateModuleTypeEnum = OperateModuleTypeEnum.MARKETING_ACTIVITY_MANAGEMENT;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String name = paasFormDataEntityExt.getData().getString(WriteOffRecordEnum.ORDER_NO.getAttr());
                    logName.add(name);
                }
                break;
            case PROMOTION_ACTIVITY_MANAGEMENT:
                operateModuleTypeEnum = OperateModuleTypeEnum.PROMOTION_ACTIVITY_MANAGEMENT;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String name = paasFormDataEntityExt.getData().getString(PromotionActivityManagementEnum.NAME.getAttr());
                    logName.add(name);
                }
                break;
            case RETURN:
                operateModuleTypeEnum = OperateModuleTypeEnum.RETURN;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    StringBuilder sb = new StringBuilder();
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    JSONObject data = paasFormDataEntityExt.getData();
                    String name = data.getString(ReturnEnum.RETURN_NAME.getAttr());
                    String log = name == null ? serialNo : sb.append(serialNo).append("（").append(name).append("）").toString();
                    logName.add(log);
                }
                break;
            case ORDER:
                operateModuleTypeEnum = OperateModuleTypeEnum.ORDER;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt contractES : paasFormDataESList){
                    StringBuilder sb = new StringBuilder();
                    String serialNo = contractES.getSerialNo();
                    JSONObject data = contractES.getData();
                    String name = data.getString(OrderEnum.ORDER_NO.getAttr());
                    String log = name == null ? serialNo : sb.append(serialNo).append("（").append(name).append("）").toString();
                    logName.add(log);
                }
                break;
            case DISTRIBUTOR_COMMUNICATE:
                operateModuleTypeEnum = OperateModuleTypeEnum.DISTRIBUTOR_COMMUNICATE;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    JSONObject data = paasFormDataEntityExt.getData();
                    String name = FastJsonHelper.getStringOrDefaultFromFormData(data, DistributorCommunicateEnum.DISTRIBUTOR_NAME_LINK_TEXT.getAttr(), "");
                    logName.add(name);
                }
                break;
            case DISTRIBUTOR_MANAGEMENT:
                subRefTypeEnum = XbbRefTypeEnum.getByCode(formDataListDTO.getSubBusinessType());
                // 生成日志(导出)
                operateModuleTypeEnum = OperateModuleTypeEnum.DISTRIBUTOR;
                typeName = subRefTypeEnum.getName();
                for(PaasFormDataEntityExt customerES : paasFormDataESList){
                    JSONObject data = customerES.getData();
                    String name = FastJsonHelper.getStringOrDefaultFromFormData(data, DistributorEnum.NAME.getAttr(), "");
                    logName.add(name);
                }
                break;
            case DISTRIBUTOR_CONTACT:
                operateModuleTypeEnum = OperateModuleTypeEnum.DISTRIBUTOR_CONTACT;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    JSONObject data = paasFormDataEntityExt.getData();
                    String name = FastJsonHelper.getStringOrDefaultFromFormData(data, DistributorEnum.NAME.getAttr(), "");
                    logName.add(name);
                }
                break;
            case ORDER_OUTSTOCK:
                operateModuleTypeEnum = OperateModuleTypeEnum.ORDER_OUTSTOCK;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case DISTRIBUTOR_ACCOUNT:
                operateModuleTypeEnum = OperateModuleTypeEnum.DISTRIBUTOR_ACCOUNT;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    JSONObject data = paasFormDataEntityExt.getData();
                    String name = FastJsonHelper.getStringOrDefaultFromFormData(data, DistributorAccountEnum.NAME.getAttr(), "");
                    logName.add(name);
                }
                break;
            case COST_ADJUST:
                operateModuleTypeEnum = OperateModuleTypeEnum.COST_ADJUST;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String serialNo = paasFormDataEntityExt.getSerialNo();
                    logName.add(serialNo);
                }
                break;
            case KNOWLEDGE_BASE:
                operateModuleTypeEnum = OperateModuleTypeEnum.KNOWLEDGE_BASE;
                typeName = xbbRefTypeEnum.getName();
                for(PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList){
                    String title = paasFormDataEntityExt.getData().getString(KnowledgeDataEnum.TITLE.getAttr());
                    logName.add(title);
                }
                break;
            case COMPETITOR:
                operateModuleTypeEnum = OperateModuleTypeEnum.COMPETITOR;
                typeName = xbbRefTypeEnum.getName();
                for (PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList) {
                    JSONObject data = paasFormDataEntityExt.getData();
                    String name = data.getString(CompetitorEnum.NAME.getAttr());
                    logName.add(name);
                }
                break;
            case SERVICE_PROJECT:
                operateModuleTypeEnum = OperateModuleTypeEnum.SERVICE_PROJECT;
                typeName = xbbRefTypeEnum.getName();
                for (PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList) {
                    JSONObject data = paasFormDataEntityExt.getData();
                    //ServiceProjectEnum.NAME.getAttr()
                    String name = data.getString("text_1");
                    logName.add(name);
                }
                break;
            case WORK_ORDER_V2:
                operateModuleTypeEnum = OperateModuleTypeEnum.WORK_ORDER_V2;
                typeName = xbbRefTypeEnum.getName();
                for (PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList) {
                    JSONObject data = paasFormDataEntityExt.getData();
                    String name = data.getString("text_1");
                    logName.add(name);
                }
                break;
            case RECEIPT_ORDER:
                operateModuleTypeEnum = OperateModuleTypeEnum.RECEIPT_ORDER;
                typeName = xbbRefTypeEnum.getName();
                for (PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList) {
                    JSONObject data = paasFormDataEntityExt.getData();
                    String title = data.getString(ReceiptOrderEnum.RECEIPT_ORDER_NO.getAttr());
                    logName.add(title);
                }
                break;
            case SLA_LOG:
                operateModuleTypeEnum = OperateModuleTypeEnum.SLA_LOG;
                typeName = xbbRefTypeEnum.getName();
                for (PaasFormDataEntityExt paasFormDataEntityExt : paasFormDataESList) {
                    JSONObject data = paasFormDataEntityExt.getData();
                    String title = data.getString(FieldTypeEnum.SERIALNO.getAlias());
                    logName.add(title);
                }
                break;
            default:
        }
         logMemo = StringUtils.join(logName, "，");
        //自定义导出日志特殊处理
        boolean excelPartialFlag = null!=formDataListDTO.getExcelPartialExportPojo()
                && Objects.equals(formDataListDTO.getExcelPartialExportPojo().getPartialExport(),1);
        if(excelPartialFlag){
            logMemo = ExcelLogUtil.excelPartialExportLog(logMemo,formDataListVO.getExplainMap(),formDataListDTO.getExcelPartialExportPojo().getPartialSelectAttrs());
        }

        return new ExcelLogPojo(typeName, operateModuleTypeEnum, logMemo);

    }


    @Override
    @Async(value = "sysLogThreadPool")
    public void log(int total, Long dataId, FormDataListDTO formDataListDTO, ExcelLogPojo excelLogPojo) {

        try {
            UserVO userVO = formDataListDTO.getLoginUser();
            String httpHeader = formDataListDTO.getHttpHeader();
            String logMemo = excelLogPojo.getLogMemo();
            String typeName = excelLogPojo.getTypeName();
            OperateModuleTypeEnum operateModuleTypeEnum = excelLogPojo.getOperateModuleTypeEnum();
            String userName = userVO.getName();
            String userId = userVO.getUserId();
            String corpid = userVO.getCorpid();
            if(total > 1){
                String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_BATCH_EXPORT), userName, typeName, total);
                List<InfoArrPojo> infoArrPojoList = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                InfoArrPojo infoArrPojo = new InfoArrPojo();
                infoArrPojo.setTitle(I18nMessageUtil.getMessage(StringConstant.EXPORT_SUCCESS));
                infoArrPojo.setContent(logMemo);
                infoArrPojoList.add(infoArrPojo);
                mongoLogHelp.buildLog4InfoArrWithoutAsync(infoArrPojoList, corpid, userId, userName, operateModuleTypeEnum, OperateTypeEnum.EXPORT,
                        "", "", memo, httpHeader);
            } else if (Objects.equals(total, 1)){
                String memo = String.format(I18nMessageUtil.getMessage(LogMemoConstant.LOG_MEMO_USER_EXPORT), userName, typeName, logMemo);
                mongoLogHelp.buildLogWithoutAsync(corpid, userId, userName, operateModuleTypeEnum, OperateTypeEnum.EXPORT,
                        dataId.toString(), logMemo, memo, httpHeader);
            }
        } catch (Exception e) {
            LOGGER.error("com.xbongbong.paas.service.impl.ExcelLogServiceImpl.log error", e);
        }
    }

}
